﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Jzy.Model.Common;
using Jzy.Model.HR;
using Jzy.BLL.HR;

using System.Collections.Generic;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Frame.Lib.Core.User;
using Frame.Lib.Core.Attach;
using Frame.Lib.Enums;
using Frame.Lib.Core.Area;

public partial class HumanResource_Employee_Edit : PageEdit
{
    private EmployeeBO Employee
    {
        get
        {
            return EmployeeBO.Instance<EmployeeBO>();
        }
    }

    private UserBO SUser
    {
        get
        {
            return UserBO.Instance<UserBO>();
        }
    }

    protected string AreaCode;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            EnumInfo.BindList(Gender, typeof(Gender));
            EnumInfo.BindList(Status, typeof(EmployeeStatus));

            if (!string.IsNullOrEmpty(this.PKID))
            {
                DispInfo();
            }
                
        }
        AddAttr();
        this.IncludeJsFile("calendar", JsFile.Calendar);
    }

    private void DispInfo()
    {
        //UserInfo userInfo = User.GetInfo(this.PKID);
        EmployeeInfo empInfo = Employee.GetInfo(this.PKID);
        if (empInfo != null)
        {
            EntityUtil.SetControl(pnlMain, empInfo);
            imgPhotoUrl.ImageUrl = empInfo.PhotoAbsPath;
            txtCode.Text = empInfo.Code;
            txtTitle.Text = empInfo.Title;
            if (empInfo.Birthday != null)
            {
                Birthday.Text = empInfo.Birthday.Value.ToString("yyyy-MM-dd");
            }
            if (empInfo.HireDate != null)
            {
                HireDate.Text = empInfo.HireDate.Value.ToString("yyyy-MM-dd");
            }

            LoginName.Text = empInfo.UserInfo.LoginName;
            DeptID.Value = StrUtil.MergeToStr(empInfo.UserInfo.DeptIDs, ",");
            DeptName.Text = empInfo.UserInfo.DeptName;

            //if (empInfo.LeaderID != null)
            //{
            //    LeaderID.Value = empInfo.LeaderID.ToString();
            //    LeaderName.Text = new DeptBO().GetName(empInfo.LeaderID.Value);
            //} 

            AreaCode = empInfo.AreaCode.ToString();


        }
    }

    private void AddAttr()
    {
        WebUtil.SetTimeBox(this.HireDate, false);
        WebUtil.SetTimeBox(this.Birthday, false);

        JSUtil.ShowTreeDlg(this.DeptID, DeptName, "Dept", "选择部门", false);
        JSUtil.ShowDeptUserTreeDlg(LeaderID, LeaderName, true, false);
       
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        //bool isAdd = string.IsNullOrEmpty(this.PKID);
        string loginName = LoginName.Text.Trim();
        bool flag = SUser.ExistField("LoginName", loginName, this.PKID);
        if (flag)
        {
            JSUtil.Alert("该登录名已存在");
            return;
        }        

        UserInfo userInfo = this.SUser.GetInfo(this.PKID);
        userInfo.LoginName = LoginName.Text.Trim();
        userInfo.Name = Name.Text.Trim();
        if (string.IsNullOrEmpty(this.PKID))
        {
            userInfo.Status = (int)UserStatus.启用;
            userInfo.Password = userInfo.LoginName;            
        }
        userInfo.DeptIDs = StrUtil.SplitToInt(DeptID.Value);

        SUser.Save(userInfo);       

        EmployeeInfo empInfo = this.Employee.GetInfo(userInfo.ID);
        bool isAdd = false;
        if (empInfo ==null )
        {
            isAdd = true;
            empInfo = new EmployeeInfo();
            empInfo.ID = userInfo.ID;
            empInfo.InputTime = DateTime.Now;
            empInfo.AllowEdit = true;
        }
        EntityUtil.SetEntity(pnlMain, empInfo);

        if (!string.IsNullOrEmpty(Request.Form["AreaCode"]))
        {
            empInfo.AreaCode =  int.Parse(Request.Form["AreaCode"]);
            
            List<int> areaIDs = new List<int>();
            areaIDs.Add(empInfo.AreaCode.Value / 10000 * 10000);//上级地区
            areaIDs.Add(empInfo.AreaCode.Value);
            AreaBO area = new AreaBO();
            List<string> areaName = area.GetList(areaIDs).OrderBy(T => T.ID).Select(T => T.Name).ToList();
            empInfo.AreaName = areaName[0] + " " + areaName[1];
        }
        empInfo.Title = txtTitle.Text.Trim();
        empInfo.Code = txtCode.Text.Trim();

        
       
        if (filPhoto.PostedFile.ContentLength > 0)
        {
            string strFileExtension = System.IO.Path.GetExtension(filPhoto.PostedFile.FileName).ToLower();
            if (strFileExtension != ".gif" && strFileExtension != ".jpg")
            {
                JSUtil.Alert("图片格式不对！");
                return;
            }

            UserImage userImage = new UserImage("Employee");
            if (string.IsNullOrEmpty(empInfo.Photo))
            {
                empInfo.Photo = userImage.Save(filPhoto.PostedFile, 100, 140);
            }
            else
            {
                userImage.Save(filPhoto.PostedFile, 100, 140, empInfo.Photo);
            }
        }

        this.Employee.Save(empInfo, isAdd);

        if (string.IsNullOrEmpty(this.PKID))
        {
            Response.Redirect("List.aspx");
        }
        else
        {
            this.Alert("保存成功");
            if (empInfo.AreaCode != null)
            {
                this.AreaCode = empInfo.AreaCode.ToString();
            }
            if (!string.IsNullOrEmpty(empInfo.Photo))
            {
                imgPhotoUrl.ImageUrl = empInfo.Photo;
            }
        }


        //EntityUtil.SetEntity(pnlMain,
    }
}
